package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.dto.DishDto;
import com.zrrd.reggie.bean.dto.SetmealDto;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        if(name!= null&& !name.equals(" ")){
            wrapper.like("name",name);
        }
        setmealService.page(pageInfo,wrapper);
        Page<SetmealDto> dtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> setmealList = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList=new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto dto=new SetmealDto();
            BeanUtils.copyProperties(setmeal,dto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category!=null){
                dto.setCategoryName(category.getName());
            }
            setmealDtoList.add(dto);
        }
        dtoPage.setRecords(setmealDtoList);
        return R.sucess(dtoPage);
    }
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public  R<String> saveSetmealWithDish(@RequestBody SetmealDto dto){
        setmealService.saveSetmealWithDish(dto);
        return R.sucess("新增成功！");
    }
    @GetMapping("/{id}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<SetmealDto> getSetmealWithDish(@PathVariable("id") Long id){
        return R.sucess(setmealService.getSetmealWithDish(id));
    }
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateSetmealWithDish(@RequestBody SetmealDto dto){
        setmealService.updateSetmealWithDish(dto);
        return R.sucess("修改成功");
    }
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateStatus(@PathVariable("status") Integer status, Long[] ids){
        List<Setmeal> setmealList=new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal=new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealList.add(setmeal);
        }
        return setmealService.updateBatchById(setmealList)?R.sucess("修改成功"):R.error("修改失败");
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmealWithDish(Long[] ids){

        List<Long> list = Arrays.asList(ids);
        for (Long l : list) {
            if (setmealService.getById(l).getStatus()!=0){
                return R.error("不能删除正在售卖的套餐");
            }
            setmealService.removeById(l);
            QueryWrapper<SetmealDish> wrapper=new QueryWrapper<>();
            wrapper.eq("setmeal_id",l);
            setmealDishService.remove(wrapper);
            }
        return R.sucess("删除成功");

    }
    @Cacheable(value = "setmealCache",key ="#setmeal.categoryId+'_'+#setmeal.status" )
    @GetMapping("list")
    public R<List> list(Setmeal setmeal){
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        wrapper.eq("category_id",setmeal.getCategoryId());
        wrapper.eq("status",1);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.sucess(setmealList);
    }
    @GetMapping("/dish/{id}")
    public R<List> getDishListById(@PathVariable("id") Long id){
        QueryWrapper<SetmealDish> wrapper=new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        List<SetmealDish> setmealDishList=setmealDishService.list(wrapper);
        return R.sucess(setmealDishList);
    }
}
